package org.jash.shop2112.product

import android.content.Context
import android.widget.ImageView
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.google.gson.Gson
import com.hyphenate.helpdesk.easeui.util.IntentBuilder
import com.youth.banner.loader.ImageLoader
import org.jash.shop2112.databinding.ActivityDetailBinding
import org.jash.shop2112.databinding.AttributeItemBinding
import org.jash.shop2112.mvvm.BaseActivity

@Route(path = "/shop/detail")
class DetailActivity : BaseActivity<ActivityDetailBinding,DetailViewModel>() {
    @Autowired
    @JvmField
    var id = 0
    override fun initData() {
        ARouter.getInstance().inject(this)
        viewModel.goodsLiveData.observe(this) {
            binding.goods = it
            it.goods_attribute?.forEach { (k, v) ->
                val item = AttributeItemBinding.inflate(layoutInflater, binding.attribute, true)
                item.text.hint = k
                item.menu.setSimpleItems(v.toTypedArray())
            }
        }
        viewModel.addCartLiveData.observe(this) {
            Toast.makeText(this, it, Toast.LENGTH_SHORT).show()
        }
        viewModel.loadGoods(id)
    }

    override fun initView() {
        binding.banner.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                imageView?.let {
                    Glide.with(it)
                        .load(path)
                        .into(it)
                }
            }
        })
        binding.addCart.setOnClickListener {
            viewModel.addCart(id)
        }
        setSupportActionBar(binding.toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        binding.buy.setOnClickListener {
            ARouter.getInstance()
                .build("/shop/pay")
                .withString("info", "${binding.goods?.goods_desc} x 1")
                .withFloat("price", binding.goods?.goods_default_price ?: 0f)
                .navigation()
        }
        binding.agent.setOnClickListener {
            startActivity(IntentBuilder(this).setTitleName("商城客服").setServiceIMNumber("kefuchannelimid_725078").build())
        }
    }

}